package org.jshand.jxc.business;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

import org.jshand.jxc.business.component.JComponentPanel;
import org.jshand.jxc.business.component.JTablePanel;
import org.jshand.jxc.business.game.SnakeGamePanel;
import org.jshand.jxc.business.layout.BorderLayoutPanel;
import org.jshand.jxc.util.DimensionUtils;
import org.jshand.jxc.util.DimensionUtils.BusiDime;

public class MainFrame extends JFrame implements ActionListener  {

	private static final long serialVersionUID = 1L;
	
	private final static int width = 800;
	private final static int height = 500;

	private JMenuItem snakeMenu;
	private JMenuItem jtableMenu;
	private JMenuItem jcomponentMenu;

	
	private JMenuItem borderLayoutMenu;
	private JMenuItem flowLayoutMenu;
	private JMenuItem cardLayoutMenu;
	private JMenuItem gridLayoutMenu;

	
	private JMenuItem metalLookAndFeelMenu;
	private JMenuItem windowsLookAndFeelMenu;
	private JMenuItem motifLookAndFeelMenu;
	private JMenuItem nimbusLookAndFeelMenu;

	JPanel statusPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
	JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT,0,0));
	
	
	List lookAndFeelList = new ArrayList();
	
	public MainFrame() throws HeadlessException {
		super();
		setSize(width,height);
		this.setLayout(new BorderLayout());
		
		BusiDime xy = DimensionUtils.getXY(width, height);
		
		setLocation(xy.getA(),xy.getB());

		initMenuBar();
		initStatuPanel();
//		this.add(new JTablePanel());
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setResizable(false);
		
		setVisible(true);

	}

	
	private void initStatuPanel() {
		statusPanel.add(new JLabel("系统准备就绪....."));
		this.add(statusPanel, BorderLayout.SOUTH);
	}


	private void initMenuBar() {
		JMenuBar mbar = new JMenuBar();
		
		JMenu sysMenu = new JMenu("系统");
		snakeMenu = new JMenuItem("贪吃蛇");
		snakeMenu.addActionListener(this);
		sysMenu.add(snakeMenu);
		mbar.add(sysMenu);

		//控件 
		JMenu compMenu = new JMenu("控件");
		jcomponentMenu = new JMenuItem("JComponent");
		jcomponentMenu.addActionListener(this);
		compMenu.add(jcomponentMenu);
		
		jtableMenu = new JMenuItem("JTable表格控件");
		jtableMenu.addActionListener(this);
		compMenu.add(jtableMenu);
		mbar.add(compMenu);
		
		
		JMenu layoutMenu = new JMenu("常用布局");
		borderLayoutMenu = new JMenuItem("边界布局");
		borderLayoutMenu.addActionListener(this);
		layoutMenu.add(borderLayoutMenu);
		
		flowLayoutMenu = new JMenuItem("流式布局");
		flowLayoutMenu.addActionListener(this);
		layoutMenu.add(flowLayoutMenu);
		
		cardLayoutMenu = new JMenuItem("卡片布局");
		cardLayoutMenu.addActionListener(this);
		layoutMenu.add(cardLayoutMenu);
		
		gridLayoutMenu = new JMenuItem("网格布局");
		gridLayoutMenu.addActionListener(this);
		layoutMenu.add(gridLayoutMenu);
		
		mbar.add(layoutMenu);
		
		
		JMenu lookAndFellMenu = new JMenu("外观(LookAndFell)");
		
		metalLookAndFeelMenu = new JCheckBoxMenuItem("Metal风格",true);
		metalLookAndFeelMenu.addActionListener(this);
		lookAndFellMenu.add(metalLookAndFeelMenu);
		
		windowsLookAndFeelMenu = new JCheckBoxMenuItem("Motif风格");
		windowsLookAndFeelMenu.addActionListener(this);
		lookAndFellMenu.add(windowsLookAndFeelMenu);
		
		motifLookAndFeelMenu = new JCheckBoxMenuItem("Motif风格");
		motifLookAndFeelMenu.addActionListener(this);
		lookAndFellMenu.add(motifLookAndFeelMenu);
		
		nimbusLookAndFeelMenu = new JCheckBoxMenuItem("Nimbus风格");
		nimbusLookAndFeelMenu.addActionListener(this);
		lookAndFellMenu.add(nimbusLookAndFeelMenu);
		
		mbar.add(lookAndFellMenu);
		
		this.setJMenuBar(mbar);
		
	}

	public MainFrame(String title) throws HeadlessException {
		this();
		setTitle(title);
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		this.getContentPane().removeAll();
		if(source == snakeMenu){
			System.out.println("贪吃蛇");
			this.getContentPane().add(new SnakeGamePanel(), BorderLayout.CENTER);
		}else if(source == jtableMenu ){
			System.out.println("Jtable");
			this.getContentPane().add(new JTablePanel(), BorderLayout.CENTER);
		}else if(source == jcomponentMenu){
			this.getContentPane().add(new JComponentPanel(), BorderLayout.CENTER);
		}
		
		//布局
		else if(source == borderLayoutMenu){
			this.getContentPane().add(new BorderLayoutPanel(), BorderLayout.CENTER);
		}else if(source == flowLayoutMenu){
			this.getContentPane().add(new BorderLayoutPanel(), BorderLayout.CENTER);
		}else if(source == cardLayoutMenu){
			this.getContentPane().add(new BorderLayoutPanel(), BorderLayout.CENTER);
		}else if(source == gridLayoutMenu){
			this.getContentPane().add(new BorderLayoutPanel(), BorderLayout.CENTER);
		}
		this.revalidate();
		this.repaint();
		
		
		//外观选择
		if(source == metalLookAndFeelMenu){
			setUI(1);
		}else if(source == windowsLookAndFeelMenu){
			setUI(2);
		}else if(source == motifLookAndFeelMenu){
			setUI(3);
		}else if(source == nimbusLookAndFeelMenu){
			setUI(4);
		}
		
	}

	
	
	
	public void setUI(int type){
		try {
			switch (type) {
				case 1:
					//1、（官方）MetalLookAndFeel（Swing 默认的 LookAndFeel）：
					UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
					break;
				case 2:
					//2、（官方）WindowsLookAndFeel：
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
					break;
				case 3:
					//3、（官方）MotifLookAndFeel：
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
					break;
				case 4:
					//4、（官方）NimbusLookAndFeel：
					UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
					break;
				default:
					break;
			}

			SwingUtilities.updateComponentTreeUI(this);
			
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		} catch (InstantiationException e1) {
			e1.printStackTrace();
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (UnsupportedLookAndFeelException e1) {
			e1.printStackTrace();
		}
	}
	
	
	public void setStatusText(String text){
		statusPanel.removeAll();
		statusPanel.add(new JLabel(text));
	}
	
	
	
}
